package es.ocu.uxxiac.appnotas.content;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class CalificacionesParserSax {
	private URL calificacionesUrl;
	
	private InputStream calificacionesInputStream;
	 
    public CalificacionesParserSax(String url)
    {
        try
        {
            this.calificacionesUrl = new URL(url);
        }
        catch (MalformedURLException e)
        {
            throw new RuntimeException(e);
        }
        try
        {
        	calificacionesInputStream = calificacionesUrl.openConnection().getInputStream();
        }
        catch (IOException e)
        {
            throw new RuntimeException(e);
        }
    }
    
    public CalificacionesParserSax(InputStream input)
    {
        try
        {
            this.calificacionesInputStream = input;
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }
 
    public ArrayList<Calificacion> parse()
    {
        SAXParserFactory factory = SAXParserFactory.newInstance();
 
        try
        {
            SAXParser parser = factory.newSAXParser();
            CalificacionesHandler handler = new CalificacionesHandler();
            parser.parse(this.calificacionesInputStream, handler);
            return handler.getCalificaciones();
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }
}
